package httpgui;

import java.io.IOException;
import java.net.URL;
import java.util.Map;
import java.util.ResourceBundle;

import http.OKHttpImpl;
import http.OKHttpImpl.HttpCallBack;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.control.Button;
import javafx.scene.control.ComboBox;
import javafx.scene.control.Tab;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import tools.jsonparse.JacksonParse;

public class HttpController implements Initializable,HttpCallBack {
	
	@FXML
	private VBox rootVBox;
	@FXML
	private VBox vboxParames;
	
	@FXML
	private VBox vboxHeaders;
	
	@FXML
	private ComboBox<String> methodComboBox;
	
	@FXML
	private Button btnParamers;
	
	@FXML
	private Button btnHeaders;
	
	@FXML
	private Button btnRequest;
	@FXML
	private TextArea headTextArea;
	
	@FXML
	private TextArea bodyTextArea;
	
	
	@FXML
	private TextArea requestTextArea;
	
	
	@FXML
	private TextField urlTextField;
	
	@FXML
	private Tab tabStatus;
	
	ObservableList<String> methodList=FXCollections.observableArrayList("GET","POST");
	

	@Override
	public void initialize(URL location, ResourceBundle resources) {
		// TODO Auto-generated method stub
		methodComboBox.setValue(methodList.get(0));
		methodComboBox.setItems(methodList);
		
	}
	@FXML
	private void paramers() throws IOException {
		HBox hbox=null;
		int size=vboxParames.getChildren().size();
		if(size>0) {
			vboxParames.getChildren().clear();
		}else {
			hbox=(HBox) PageTool.getPaneFromFxml("URLParamsNoClose.fxml");
			vboxParames.getChildren().add(hbox);
		}
	}
	
	@FXML
	private void headers() throws IOException {
		HBox hbox=null;
		int size=vboxHeaders.getChildren().size();
		if(size>0) {
			vboxHeaders.getChildren().clear();
		}else {
			hbox=(HBox) PageTool.getPaneFromFxml("URLHeadersNoClose.fxml");
			vboxHeaders.getChildren().add(hbox);
		}
	}
	
	@FXML
	private void request() throws IOException {
		OKHttpImpl okHttpImpl=new OKHttpImpl();
//		okHttpImpl.setHttpCallBack(this);
//		setHeaders(okHttpImpl);
//		String urlPtah=urlTextField.getText();
//		urlPtah=setParamers(urlPtah);
//		okHttpImpl.getEnqueue(urlTextField.getText());
		okHttpImpl.postString("");
		
	}
	
	private String setParamers(String urlPath) {
		int size=vboxParames.getChildren().size();
		if(size>0) {
			if(size>1) {
				urlPath=urlPath+"?";
			}
			for(int i=0;i<size;i++) {
				HBox hbox=(HBox) vboxParames.getChildren().get(i);
				TextField tKey=(TextField) hbox.lookup("#textKey");
				TextField tValue=(TextField) hbox.lookup("#textValue");
				if(tKey!=null && tValue!=null) {
					String keyStr=tKey.getText();
					String valueStr=tValue.getText();
					if(keyStr.length()>0 && valueStr.length()>0) {
						if(size==1) {
							urlPath=urlPath+"?"+keyStr+"="+valueStr;
						}else {
							if(i==0) {
								urlPath=urlPath+keyStr+"="+valueStr;
							}else {
								urlPath=urlPath+"&&"+keyStr+"="+valueStr;
							}
							
						}
					}
				}
				
			}
		}
		return urlPath;
	}
	
	
	private void setHeaders(OKHttpImpl okHttpImpl) {
		int size=vboxHeaders.getChildren().size();
		if(size>0) {
			for(int i=0;i<size;i++) {
				HBox hbox=(HBox) vboxHeaders.getChildren().get(i);
				TextField tKey=(TextField) hbox.lookup("#textKey");
				TextField tValue=(TextField) hbox.lookup("#textValue");
				if(tKey!=null && tValue!=null) {
					String keyStr=tKey.getText();
					String valueStr=tValue.getText();
					if(keyStr.length()>0 && valueStr.length()>0) {
						okHttpImpl.setHeader(keyStr, valueStr);
					}
				}
				
			}
		}
	}
	@Override
	public void success(Map<Integer, String> map) {
		if(map!=null && map.size()==5) {
			bodyTextArea.setText(new JacksonParse().strToPretty(map.get(1)));
			headTextArea.setText(map.get(2));
			tabStatus.setText("status:"+map.get(3)+",time:"+map.get(4));
			requestTextArea.setText(map.get(5));
		}
	}
	@Override
	public void error(Map<Integer, String> map) {
		if(map!=null && map.size()==5) {
			bodyTextArea.setText(new JacksonParse().strToPretty(map.get(1)));
			headTextArea.setText(map.get(2));
			tabStatus.setText("status:"+map.get(3)+",time:"+map.get(4));
			requestTextArea.setText(map.get(5));
		}
	}
	
	
	
	

}
